package com.shiguo365.market.display.activity;

import android.content.Intent;
import android.os.Handler;


import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.shiguo365.market.R;
import com.shiguo365.market.application.MyApplication;
import com.shiguo365.market.bean.Product;
import com.shiguo365.market.display.adapter.OrderAdapter;
import com.shiguo365.market.display.adapter.OrderListAdapter;
import com.shiguo365.market.display.base.BaseActivity;
import com.shiguo365.market.display.listener.NavigationFinishClickListener;
import com.shiguo365.market.display.util.RefreshUtils;
import com.shiguo365.market.display.util.ToastUtils;
import com.shiguo365.market.display.view.IOrderView;
import com.shiguo365.market.presenter.contract.IOrderPresenter;
import com.shiguo365.market.presenter.implement.OrderPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener,IOrderView,OrderAdapter.OnItemClickLitener {

    private String TAG="OrderActivity";

    private LinearLayoutManager mLayoutManager;

    OrderAdapter mAdapter;

    private IOrderPresenter mOrderPresenter;

    private List<JSONObject> jsonObjectList= new ArrayList<JSONObject>();;

    @BindView(R.id.order_list_refresh_layout) protected SwipeRefreshLayout refreshLayout;

    @BindView(R.id.my_recycler_view_orderlist) protected RecyclerView mRecyclerView;

    @BindView(R.id.order_list_toolbar) protected Toolbar toolbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);

        ButterKnife.bind(this); //ButterKnife初始化

        mOrderPresenter=new OrderPresenter(this,this);
        toolbar.setNavigationOnClickListener(new NavigationFinishClickListener(this));
        mAdapter = new OrderAdapter(mOrderPresenter,jsonObjectList);
        mAdapter.setOnItemClickLitener(this);
        mRecyclerView.setAdapter(mAdapter);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
//如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        mRecyclerView.setHasFixedSize(true);
//创建并设置Adapter
        RefreshUtils.initOnCreate(refreshLayout, this);
        RefreshUtils.refreshOnCreate(refreshLayout, this);
    }

    @Override
    public void onRefresh() {
        //添加刷新代码
        jsonObjectList.clear();
        String status = "";
        mOrderPresenter.getOrderByUI(MyApplication.getUID(),status);

    }

    @Override
    public void OnGetOrderOK(List<JSONObject> jsonObjectList) {

        for (int i = 0; i < jsonObjectList.size(); i++) {
            this.jsonObjectList.add(jsonObjectList.get(i));
        }

        mAdapter.notifyDataSetChanged();

        Log.d(TAG,jsonObjectList.toString());
    }

    @Override
    public void OnGetOrderFinsh() {
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void onItemClick(View view, int position) {

        JSONObject object = jsonObjectList.get(position);
        Intent intent =new Intent(this, OrderDetailActivity.class);
        String orderid = object.getString("orderid");
        intent.putExtra("oid",orderid);
        startActivity(intent);
    }

    @Override
    public void onItemLongClick(View view, int position) {

    }
}
